package com.siyoumi.app.modules.app_book.service;

import com.siyoumi.app.entity.BookOrder;
import com.siyoumi.app.modules.app_book.service.handle_order.BookOrderHandle20FunDown;
import com.siyoumi.app.modules.app_book.service.handle_order.BookOrderHandle80PriceTotal;
import com.siyoumi.app.modules.app_book.service.handle_order.BookOrderHandle85GiveFun;
import com.siyoumi.app.modules.app_book.service.handle_order.BookOrderHandle90SharePrice;
import com.siyoumi.component.LogPipeLine;
import com.siyoumi.exception.EnumSys;
import com.siyoumi.util.XReturn;
import com.siyoumi.validator.XValidator;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单处理
 */
public class BookOrderHandle
        extends BookOrderHandleAbs {
    static public BookOrderHandle of() {
        return new BookOrderHandle();
    }

    @Override
    public XReturn orderBeforeCheck() {
        LogPipeLine log = getLog();
        log.setLogMsg("订单预检查======BEGIN");
        for (BookOrderHandleAbs handle : getHandleList()) {
            handle.setVo(getVo());
            handle.setListDay(getListDay());

            log.setLogMsgFormat("{0}---BEGIN", handle.getTitle());
            XReturn r = handle.orderBeforeCheck();
            XValidator.err(r);
            log.setLogMsgFormat("{0}---END", handle.getTitle());
        }

        log.setLogMsg("订单预检查======END");
        return EnumSys.OK.getR();
    }

    protected List<BookOrderHandleAbs> getHandleList() {
        List<BookOrderHandleAbs> list = new ArrayList<>();
        list.add(new BookOrderHandle20FunDown());
        list.add(new BookOrderHandle80PriceTotal());
        list.add(new BookOrderHandle85GiveFun());
        list.add(new BookOrderHandle90SharePrice());
        return list;
    }

    @Override
    public String getTitle() {
        return "";
    }

    @Override
    public XReturn handle(BookOrder entityOrder) {
        LogPipeLine log = getLog();
        log.setLogMsg("订单处理======BEGIN");
        log.setAppendData("log_str_00", entityOrder.getKey());
        XReturn r = EnumSys.OK.getR();
        for (BookOrderHandleAbs handle : getHandleList()) {
            handle.setVo(getVo());
            handle.setListDay(getListDay());

            log.setLogMsgFormat("{0}---BEGIN", handle.getTitle());
            r = handle.handle(entityOrder);
            log.setLogMsgFormat("{0}---END", handle.getTitle());
            if (r.err()) { //报错跳出循环
                break;
            }
        }
        log.setLogMsg("订单处理======END");
        XValidator.err(r);

        return EnumSys.OK.getR();
    }
}
